﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.IO;
using System.Data.OleDb;

public partial class cms_Admin_Users_tbUser : System.Web.UI.UserControl
{
    #region[Declare varian]
    static string ID = "";
    static string insert = "";
    static string Level = "";
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { Viewphongban(); Viewchucvu(); Viewlist(); Showthutu(); Viewlistdrop(); }
    }
    #endregion
    #region[Viewlistdrop]
    void Viewlistdrop()
    {
        string Chuoi = "";
        drlChuyennhom.Items.Clear();
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByAll();
        drlChuyennhom.Items.Add(new ListItem("Nhóm chuyển đến","0"));
        drlChuyennhom.Items.Add(new ListItem("Chuyền thành nhóm cao nhất", "-1"));
        for (int i = 0; i < list.Count; i++)
        {
            Chuoi = "";
            for (int j = 1; j < list[i].uLevel.Length / 5; j++)
            {
                Chuoi = Chuoi + "";
            }
            drlChuyennhom.Items.Add(new ListItem(Chuoi.ToString() + list[i].uUser + " - " + list[i].uName, list[i].uLevel));
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Viewphongban]
    void Viewphongban()
    {
        drlPhongban.Items.Clear();
        List<tbDepartmentsDATA> list = tbDepartmentsDB.tbDepartments_GetByAll();
        drlPhongban.Items.Add(new ListItem("---Chọn phòng ban---","0"));
        drlBophan.Items.Clear();
        drlBophan.Items.Add(new ListItem("===Chọn bộ phận===", "0"));
        for (int i = 0; i < list.Count; i++)
        {
            drlPhongban.Items.Add(new ListItem(list[i].dpName, list[i].dpID));
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Viewchucvu]
    void Viewchucvu()
    {
        drlChucvu.Items.Clear();
        List<tbPositionsDATA> list = tbPositionsDB.tbPositions_GetByAll();
        drlChucvu.Items.Add(new ListItem("---Chọn chức vụ---", "0"));
        for (int i = 0; i < list.Count; i++)
        {
            drlChucvu.Items.Add(new ListItem(list[i].psName, list[i].psID));
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Showthutu]
    void Showthutu()
    {
        drlThutu.Items.Clear();
        for (int i = 1; i < 501; i++)
        {
            drlThutu.Items.Add(i.ToString());
        }
    }
    #endregion
    #region[Viewlist]
    void Viewlist()
    {
        List<tbUsersDATA> list = new List<tbUsersDATA>();
        list = tbUserDB.tbUsers_GetByAll();
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Add]
    protected void lbtinsertadv_Click(object sender, EventArgs e)
    {
        Viewlist();
        pnDanhsach.Visible = false;
        pnUpdate.Visible = true;
        Resetcontrol();
        insert = "true";
        Level = "";
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByMaxOrder("00000");
        if (list.Count > 0)
        {
            drlThutu.SelectedValue = Convert.ToString(int.Parse(list[0].uOrd) + 1);
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Delete]
    protected void Delete(object sender, System.EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return confirm('Bạn chắc chắn muốn xóa thông tin vừa chọn !!') ";
    }
    #endregion
    #region[Update]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        if (Test() == true)
        {
            string img_path = "";
            if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
            {
                img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
                fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
                img_path = "Users/files/" + img_path.ToString().Trim();
            }
            if (insert == "true")
            {
                List<tbUsersDATA> listcode = tbUserDB.tbUsers_GetByuUser(txtMathanhvien.Text);
                if (listcode.Count > 0)
                {
                    lblLoi.Text = "Mã nhân viên này đã có người dùng, vui lòng nhập mã khác !!";
                    return;
                }
                listcode.Clear();
                listcode = null;
                List<tbUsersDATA> list = tbUserDB.tbUsers_GetByuUser(txtUserID.Text);
                if (list.Count > 0)
                {
                    lblLoi.Text = "Acount này đã có người dùng, vui lòng nhập Acount khác !!";
                    return;
                }
                list.Clear();
                list = null;
                tbUsersDATA ObjtbUsersDATA = new tbUsersDATA();
                ObjtbUsersDATA.dpID = drlPhongban.SelectedValue;
                ObjtbUsersDATA.psID = drlChucvu.SelectedValue;
                ObjtbUsersDATA.uCode = txtMathanhvien.Text;
                ObjtbUsersDATA.uUser = txtUserID.Text;
                ObjtbUsersDATA.uPass = vmmsclass.Encodingvmms.Encode(txtPassID.Text);
                ObjtbUsersDATA.uName = txtTen.Text;
                ObjtbUsersDATA.uDate = txtNgaysinh.Text;
                ObjtbUsersDATA.uAdd = txtDiachi.Text;
                ObjtbUsersDATA.uTel = txtDienthoai.Text;
                if (Level.Length > 0)
                {
                    ObjtbUsersDATA.uLevel = Level + "00000";
                }
                else
                {
                    ObjtbUsersDATA.uLevel = "00000";
                }
                ObjtbUsersDATA.uOrd = drlThutu.SelectedValue;
                ObjtbUsersDATA.uEmail = txtEmail.Text;
                if (chkQuantri.Checked == true)
                {
                    ObjtbUsersDATA.uAdmin = "1";
                }
                else
                {
                    ObjtbUsersDATA.uAdmin = "0";
                }
                ObjtbUsersDATA.uImg = img_path;
                if (chkCongnhan.Checked == true)
                {
                    ObjtbUsersDATA.uCN = "1";
                }
                else
                {
                    ObjtbUsersDATA.uCN = "0";
                }
                ObjtbUsersDATA.paId = drlBophan.SelectedValue;
                //try
                //{
                    if (tbUserDB.tbUsers_Add(ObjtbUsersDATA))
                    {
                        WebMsgBox.Show("Thêm mới thành viên thành công !!");
                        //lblThongbao.Text = "Thêm mới thành viên thành công !!";
                        Resetcontrol();
                        insert = "";
                        Viewlist();
                        pnDanhsach.Visible = true;
                        pnUpdate.Visible = false;
                        Level = "";
                    }
                //}
                //catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
            }
            else
            {
                if (ID.Length > 0)
                {
                    List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(ID);
                    if (list.Count > 0)
                    {
                        list[0].uID = ID;
                        list[0].dpID = drlPhongban.SelectedValue;
                        list[0].psID = drlChucvu.SelectedValue;
                        list[0].uUser = txtUserID.Text;
                        if (txtPassID.Text != "")
                        {
                            list[0].uPass = vmmsclass.Encodingvmms.Encode(txtPassID.Text);
                        }
                        else
                        {
                            list[0].uPass = list[0].uPass;
                        }
                        list[0].uName = txtTen.Text;
                        list[0].uDate = txtNgaysinh.Text;
                        list[0].uAdd = txtDiachi.Text;
                        list[0].uTel = txtDienthoai.Text;
                        list[0].uLevel = Level;
                        list[0].uOrd = drlThutu.SelectedValue;
                        list[0].uEmail = txtEmail.Text;
                        if (chkQuantri.Checked == true)
                        {
                            list[0].uAdmin = "1";
                        }
                        else
                        {
                            list[0].uAdmin = "0";
                        }
                        list[0].uImg = img_path;
                        if (chkCongnhan.Checked == true)
                        {
                            list[0].uCN = "1";
                        }
                        else
                        {
                            list[0].uCN = "0";
                        }
                        list[0].paId = drlBophan.SelectedValue;
                        try
                        {
                            if (tbUserDB.tbUsers_Update(list[0]))
                            {
                                WebMsgBox.Show("Cập nhật thành viên thành công !!");
                                //lblThongbao.Text = "Cập nhật thành viên thành công !!";
                                Resetcontrol();
                                insert = "";
                                Level = "";
                                Viewlist();
                                pnDanhsach.Visible = true;
                                pnUpdate.Visible = false;
                            }
                        }
                        catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
                    }
                }
            }
        }
    }
    #endregion
    #region[Back]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {
        Viewlist();
        pnDanhsach.Visible = true;
        pnUpdate.Visible = false;
        Level = "";
        insert = "";
        Resetcontrol();
    }
    #endregion
    #region[Resetcontrol]
    void Resetcontrol()
    {
        txtUserID.Text = "";
        txtPassID.Text = "";
        txtTen.Text = "";
        txtDiachi.Text = "";
        txtDienthoai.Text = "";
        txtMathanhvien.Text = "";
        txtNgaysinh.Text = "";
        drlThutu.SelectedValue = "1";
        drlPhongban.SelectedValue = "0";
        drlChucvu.SelectedValue = "0";
        drlBophan.SelectedValue = "0";
        Level = "";
        txtEmail.Text = "";
        chkQuantri.Checked = false;
        chkCongnhan.Checked = false;
    }
    #endregion
    #region[Test]
    protected bool TestCN()
    {
        if (drlPhongban.SelectedValue == "0") { lblLoi.Text = "Chưa chọn phòng ban !!"; return false; }
        //if (drlBophan.SelectedValue == "0") { lblLoi.Text = "Chưa chọn bộ phận !!"; return false; }
        if (drlChucvu.SelectedValue == "0") { lblLoi.Text = "Chưa chọn chức vụ !!"; return false; }
        return true;
    }
    protected bool Test()
    {
        if (drlPhongban.SelectedValue == "0") { lblLoi.Text = "Chưa chọn phòng ban !!"; return false; }
        //if (drlBophan.SelectedValue == "0") { lblLoi.Text = "Chưa chọn bộ phận !!"; return false; }
        if (drlChucvu.SelectedValue == "0") { lblLoi.Text = "Chưa chọn chức vụ !!"; return false; }
        if (txtMathanhvien.Text == "") { lblLoi.Text = "Chưa nhập mã nhân viên !!"; return false; }
        if (txtPassID.Text == "" && insert == "true") { lblLoi.Text = "Chưa nhập mật khẩu nhân viên !!"; return false; }
        if (txtTen.Text == "") { lblLoi.Text = "Chưa nhập tên nhân viên !!"; return false; }
        return true;
    }
    #endregion
    #region[rptDanhsach_ItemCommand]
    protected void rptDanhsach_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Level = "";
        string ten = e.CommandName.ToString();
        ID = e.CommandArgument.ToString();
        switch (ten)
        {
            case "Edit":
                insert = "false";
                List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(ID);
                drlPhongban.SelectedValue = list[0].dpID;
                Viewbophan();
                try
                {
                    drlBophan.SelectedValue = list[0].paId;
                }
                catch { }
                drlChucvu.SelectedValue = list[0].psID;
                txtUserID.Text = list[0].uUser;
                txtTen.Text = list[0].uName;
                txtMathanhvien.Text = list[0].uCode;
                txtNgaysinh.Text = list[0].uDate;
                txtDienthoai.Text = list[0].uTel;
                txtDiachi.Text = list[0].uAdd;
                txtEmail.Text = list[0].uEmail;
                Level = list[0].uLevel;
                if (list[0].uImg.Length > 0)
                {
                    lblImg.Text = list[0].uImg.ToString();
                    ltrImg.Text = Hinhanh_resize(list[0].uImg.ToString());
                }
                if (list[0].uAdmin == "1") { chkQuantri.Checked = true; } else { chkQuantri.Checked = false; }
                if (list[0].uCN == "1") { chkCongnhan.Checked = true; } else { chkCongnhan.Checked = false; }
                drlThutu.SelectedValue = list[0].uOrd;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                break;
            case "Delete":
                tbUserDB.tbUsers_Delete(ID);
                WebMsgBox.Show("Xóa thành công thành viên !!");
                Viewlist();
                break;
            case "Add":
                List<tbUsersDATA> lists = tbUserDB.tbUsers_GetByID(ID);
                insert = "true";
                Resetcontrol();
                Level = lists[0].uLevel;
                List<tbUsersDATA> listu = tbUserDB.tbUsers_GetByMaxOrder(Level + "00000");
                if (listu.Count > 0)
                {
                    drlThutu.SelectedValue = Convert.ToString(int.Parse(listu[0].uOrd) + 1);
                }
                else
                {
                    drlThutu.SelectedValue = "1";
                }
                listu.Clear();
                listu = null;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                break;
            case "Cut":
                if (drlChuyennhom.SelectedValue != "0" && drlChuyennhom.SelectedValue!="-1")
                {
                    System.Threading.Thread.Sleep(2000);
                    //base.OnLoad(e);
                    string theTime = DateTime.Now.ToLongTimeString();
                    CheckBox chkChon;
                    Label lblID;
                    for (int i = 0; i < rptDanhsach.Items.Count; i++)
                    {
                        chkChon = (CheckBox)rptDanhsach.Items[i].FindControl("chkChon");
                        lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                        if (chkChon.Checked == true)
                        {
                            List<tbUsersDATA> listup = tbUserDB.tbUsers_GetByID(lblID.Text);
                            if (listup.Count > 0)
                            {
                                if (listup[0].uLevel != drlChuyennhom.SelectedValue)
                                {
                                    listup[0].uID = lblID.Text;
                                    listup[0].uLevel = drlChuyennhom.SelectedValue + "00000";
                                    listup[0].uOrd = listup[0].uOrd;
                                    try
                                    {
                                        tbUserDB.tbUsers_Update_Level(listup[0]);
                                    }
                                    catch { return; }
                                }
                            }
                            listup.Clear();
                            listup = null;
                        }
                    }
                    Viewlist();
                }
                else if (drlChuyennhom.SelectedValue == "-1")
                {
                    System.Threading.Thread.Sleep(2000);
                    //base.OnLoad(e);
                    string theTime = DateTime.Now.ToLongTimeString();
                    CheckBox chkChon;
                    Label lblID;
                    for (int i = 0; i < rptDanhsach.Items.Count; i++)
                    {
                        chkChon = (CheckBox)rptDanhsach.Items[i].FindControl("chkChon");
                        lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                        if (chkChon.Checked == true)
                        {
                            List<tbUsersDATA> listup = tbUserDB.tbUsers_GetByID(lblID.Text);
                            if (listup.Count > 0)
                            {
                                if (listup[0].uLevel != drlChuyennhom.SelectedValue)
                                {
                                    listup[0].uID = lblID.Text;
                                    listup[0].uLevel = "00000";
                                    listup[0].uOrd = listup[0].uOrd;
                                    try
                                    {
                                        tbUserDB.tbUsers_Update_Level(listup[0]);
                                    }
                                    catch { return; }
                                }
                            }
                            listup.Clear();
                            listup = null;
                        }
                    }
                    Viewlist();
                }
                break;
        }
    }
    #endregion 
    #region[Hinhanh_resize]
    protected string Hinhanh_resize(string imgpath)
    {
        string chuoi = "";
        if (imgpath.ToString().Trim().Length > 0)
        {
            try
            {
                if (imgpath.IndexOf(".swf") > 0)
                {
                    chuoi += "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' border='0' width='150px' height='90'>\n";
                    chuoi += "<param name='movie' value='" + imgpath + "'>\n";
                    chuoi += "<param name='AllowScriptAccess' value='always'>\n";
                    chuoi += "<param name='quality' value='High'>\n";
                    chuoi += "<embed src='" + imgpath + "'\n";
                    chuoi += "pluginspage='http://www.macromedia.com/go/getflashplayer'\n";
                    chuoi += "type='application/x-shockwave-flash'\n";
                    chuoi += "allowscriptaccess='always'\n";
                    chuoi += "width='150'\n";
                    chuoi += "height='90'>\n";
                    chuoi += "</object>\n";
                }
                else
                {
                    System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(Server.MapPath(imgpath.ToString()));
                    int sourceWidth = imgPhotoVert.Width;
                    int sourceHeight = imgPhotoVert.Height;

                    float nPercent = 0;
                    float nPercentW = 0;
                    float nPercentH = 0;
                    if (sourceWidth > 200 || sourceHeight > 250)
                    {
                        nPercentW = ((float)200 / (float)sourceWidth);
                        nPercentH = ((float)250 / (float)sourceHeight);
                        if (nPercentH < nPercentW)
                        {
                            nPercent = nPercentH;
                        }
                        else
                        {
                            nPercent = nPercentW;
                        }
                        int destWidth = (int)(sourceWidth * nPercent);
                        int destHeight = (int)(sourceHeight * nPercent);
                        chuoi = "<img src=" + imgpath.ToString().Trim() + " border=0 width=" + destWidth + " height=" + destHeight + ">";

                    }
                    else
                    {
                        int destWidth = (int)(sourceWidth);
                        int destHeight = (int)(sourceHeight);
                        chuoi = "<img src=" + imgpath.ToString().Trim() + " border=0 width=" + destWidth + " height=" + destHeight + ">";

                    }
                }
            }
            catch { }
        }
        return chuoi;
    }
    #endregion
    #region[Hienthihinhcay]
    protected string Hienthihinhcay(string name, string treecode)
    {
        string chuoi = "<img src='images/admin/Speerio_folderopen_edit.gif' border=0>";
        if (treecode.ToString().Trim().Length > 50)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 45)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 40)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 35)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 30)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 25)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 20)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 15)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 10)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 5)
        {
            chuoi = "&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else
        {
            chuoi = chuoi;
        }
        return chuoi = chuoi + "&nbsp;" + name;
    }
    #endregion
    #region[ViewNameDepartment]
    protected string ViewNameDepartment(string IdDepart)
    {
        string Chuoi = "";
        List<tbDepartmentsDATA> list = tbDepartmentsDB.tbDepartments_GetByID(IdDepart);
        if (list.Count > 0)
        {
            Chuoi = list[0].dpName;
        }
        return Chuoi;
    }
    #endregion
    #region[Bophan]
    protected string Bophan(string paId)
    {
        string Chuoi = "";
        List<tbPartsDATA> list = tbPartsDB.tbParts_GetByID(paId);
        if (list.Count > 0)
        {
            Chuoi = list[0].Name;
        }
        return Chuoi;
    }
    #endregion
    #region[ViewNameDepartment]
    protected string ViewNamePosition(string IdPos)
    {
        string Chuoi = "";
        List<tbPositionsDATA> list = tbPositionsDB.tbPositions_GetByID(IdPos);
        if (list.Count > 0)
        {
            Chuoi = list[0].psName;
        }
        return Chuoi;
    }
    #endregion
    #region[Quyen CN]
    protected bool QuyenCN(string ucn)
    {
        if (ucn == "1")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
    #region[Capnhatquyencongnhan]
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
        //base.OnLoad(e);
        string theTime = DateTime.Now.ToLongTimeString();
        CheckBox chkChon;
        Label lblID;
        for (int i = 0; i < rptDanhsach.Items.Count; i++)
        {
            chkCongnhan = (CheckBox)rptDanhsach.Items[i].FindControl("chkCongnhan");
            lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
            List<tbUsersDATA> listup = tbUserDB.tbUsers_GetByID(lblID.Text);
            if (listup.Count > 0)
            {
                listup[0].uID = lblID.Text;
                if (chkCongnhan.Checked == true)
                {
                    listup[0].uCN = "1";
                }
                else
                {
                    listup[0].uCN = "0";
                }
                try
                {
                    tbUserDB.tbUsers_Update_uCN(listup[0]);
                }
                catch { return; }
            }
            listup.Clear();
            listup = null;
        }
        Viewlist();
    }
    #endregion
    #region[Upload file Excel]
    protected void lbtCapnhatCN_Click(object sender, EventArgs e)
    {
        if (TestCN() == true)
        {
            string img_path = "";
            //Kiem tra va upload file
            if ((Path.GetExtension(file_Excel.Value) == ".xls" || Path.GetExtension(file_Excel.Value) == ".xlsx") && file_Excel.Value.Trim().Length > 0 && file_Excel.PostedFile != null && file_Excel.PostedFile.ContentLength > 0)
            {
                //try
                //{
                    //Tao duong dan neu chua ton tai
                    Directory.CreateDirectory(Server.MapPath("upload/file/"));
                    img_path = System.IO.Path.GetFileName(file_Excel.PostedFile.FileName);
                    img_path = Server.MapPath("upload/file/" + img_path.ToString().Trim()); //Duong dan + ten file
                    file_Excel.PostedFile.SaveAs(img_path);
                    string t = "0";
                    if (common.ExitSheetName(img_path, txtSheet.Text) == true)
                    {
                        //Inport danh ba vao CSDL, lay danh sach danh ba
                        OleDbConnection con;
                        if (Path.GetExtension(img_path) == ".xlsx")
                        {
                            con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + img_path + ";Extended Properties=\"Excel 8.0;HDR=YES;\"");
                        }
                        else
                        {
                            con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + img_path + ";Extended Properties=Excel 8.0;");
                        }
                        OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + txtSheet.Text + "$]", con);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        foreach (DataRow dr in dt.Rows)
                        {
                            try
                            {
                                string Mathanhvien = dr[0].ToString().Trim();
                                string Acountthanhvien = dr[1].ToString().Trim();
                                string Matkhauthanhvien = dr[2].ToString().Trim();
                                string Tenthanhvien = dr[3].ToString().Trim();
                                string Ngaysinh = dr[4].ToString().Trim();
                                string Diachi = dr[5].ToString().Trim();
                                string Dienthoai = dr[6].ToString().Trim();
                                string Email = dr[7].ToString().Trim();
                                string Thutu = dr[8].ToString().Trim();
                                List<tbUsersDATA> listukt = tbUserDB.tbUsers_GetByuUser(Acountthanhvien);
                                if (listukt.Count == 0)
                                {
                                    tbUsersDATA ObjtbUsersDATA = new tbUsersDATA();
                                    ObjtbUsersDATA.dpID = drlPhongban.SelectedValue;
                                    ObjtbUsersDATA.paId = drlBophan.SelectedValue;
                                    ObjtbUsersDATA.psID = drlChucvu.SelectedValue;
                                    ObjtbUsersDATA.uCode = Mathanhvien;
                                    ObjtbUsersDATA.uUser = Acountthanhvien;
                                    ObjtbUsersDATA.uPass = vmmsclass.Encodingvmms.Encode(Matkhauthanhvien);
                                    ObjtbUsersDATA.uName = Tenthanhvien;
                                    ObjtbUsersDATA.uDate = Ngaysinh;
                                    ObjtbUsersDATA.uAdd = Diachi;
                                    ObjtbUsersDATA.uTel = Dienthoai;
                                    if (Level.Length > 0)
                                    {
                                        ObjtbUsersDATA.uLevel = Level + "00000";
                                    }
                                    else
                                    {
                                        ObjtbUsersDATA.uLevel = "00000";
                                    }
                                    ObjtbUsersDATA.uOrd = Thutu;
                                    ObjtbUsersDATA.uEmail = Email;
                                    ObjtbUsersDATA.uAdmin = "0";
                                    ObjtbUsersDATA.uImg = "";
                                    ObjtbUsersDATA.uCN = "1";
                                    if (tbUserDB.tbUsers_Add(ObjtbUsersDATA))
                                    {
                                        t = "1";
                                    }
                                }
                                listukt.Clear();
                                listukt = null;
                            }
                            catch {
                                lblLoi.Text = "Kiếm tra định dạng dữ liệu trên file Excel, đã có lỗi xẩy ra !!";
                            }
                        }

                        //Xoa file 
                        File.Delete(img_path);
                    }
                    else
                    {
                        lblLoi.Text = "Tên sheet chưa chính xác, đề nghị kiểm tra và nhập lại !!";
                        return;
                    }
                    if (t.ToString() == "1")
                    {
                        lblThongbao.Text = "Thêm mới công nhân thành công !!";
                        Resetcontrol();
                        insert = "";
                        Viewlist();
                        pnDanhsach.Visible = true;
                        pnUpdate.Visible = false;
                        Level = "";
                    }
                    else
                    {
                        lblThongbao.Text = "Acount thêm vào đã tồn tại !!";
                        Resetcontrol();
                        insert = "";
                        Viewlist();
                        pnDanhsach.Visible = true;
                        pnUpdate.Visible = false;
                        Level = "";
                    }
                //}
                //catch
                //{
                //    lblLoi.Text = "Tên sheet chưa chính xác, đề nghị kiểm tra và nhập lại !!";
                //    return;
                //}
            }
            else
            {
                lblLoi.Text = "Chưa có file upload danh sách !!";
                return;
            }
        }
    }
    #endregion
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
        //base.OnLoad(e);
        string theTime = DateTime.Now.ToLongTimeString();
        CheckBox chkDelete;
        Label lblID;
        for (int i = 0; i < rptDanhsach.Items.Count; i++)
        {
            chkDelete = (CheckBox)rptDanhsach.Items[i].FindControl("chkDelete");
            lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
            if (chkDelete.Checked == true)
            {
                try
                {
                    tbUserDB.tbUsers_Delete(lblID.Text);
                    lblThongbao.Text = "Xóa thành công !!";
                }
                catch { return; }
            }
        }
        Viewlist();
    }
    protected void drlPhongban_SelectedIndexChanged(object sender, EventArgs e)
    {
        Viewbophan();
    }
    #region[Viewbophan]
    void Viewbophan()
    {
        drlBophan.Items.Clear();
        drlBophan.Items.Add(new ListItem("===Chọn bộ phận===", "0"));
        List<tbPartsDATA> list = new List<tbPartsDATA>();
        if (drlPhongban.SelectedValue != "0")
        {
            list = tbPartsDB.tbParts_GetBypId(drlPhongban.SelectedValue);
        }
        for (int i = 0; i < list.Count; i++)
        {
            drlBophan.Items.Add(new ListItem(list[i].Name, list[i].ID));
        }
    }
    #endregion
}
